package com.ruoyi.supervise.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.supervise.domain.BaseBulidUnitInfo;
import com.ruoyi.supervise.domain.BaseCompanyUserInfo;
import com.ruoyi.supervise.domain.BaseUnitInfo;
import com.ruoyi.supervise.domain.BaseUnitUserInfo;
import com.ruoyi.supervise.domain.ConstructionPermitRecord;
import com.ruoyi.supervise.domain.CrOperaterecordinfo;
import com.ruoyi.supervise.domain.Enterprise;
import com.ruoyi.supervise.mapper.BaseUnitInfoMapper;
import com.ruoyi.supervise.mapper.ConstructionPermitRecordMapper;
import com.ruoyi.supervise.mapper.EnterpriseMapper;
import com.ruoyi.supervise.service.IConstructionPermitRecordService;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;

/**
 * @ClassName: IConstructionPermitRecordService
 * @Description: 施工许可审查记录serviceimpl
 * @Author wtj
 * @DateTime 2020年4月13日 上午11:59:16
 */
@Service
@DataSource(value = DataSourceType.SLAVE)
public class ConstructionPermitRecordServiceImpl implements IConstructionPermitRecordService 
{
    @Autowired
    private ConstructionPermitRecordMapper constructionPermitRecordMapper;
    
    @Autowired
    private BaseUnitInfoMapper baseUnitInfoMapper;
    
    @Autowired
    private EnterpriseMapper enterpriseMapper;

	@Override
	public List<ConstructionPermitRecord> selectConstructionPermitRecordTable(ConstructionPermitRecord cpr) {
		if(StringUtils.isEmpty(cpr.getReportTimeBegin()) && !StringUtils.isEmpty(cpr.getReportTimeEnd())) {
			cpr.setReportTimeBegin("1970-01-01");
		}else if(!StringUtils.isEmpty(cpr.getReportTimeBegin()) && StringUtils.isEmpty(cpr.getReportTimeEnd())) {
			cpr.setReportTimeEnd(DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN));
		}
		if(StringUtils.isEmpty(cpr.getIssueDateBegin()) && !StringUtils.isEmpty(cpr.getIssueDateEnd())) {
			cpr.setIssueDateBegin("1970-01-01");
		}else if(!StringUtils.isEmpty(cpr.getIssueDateBegin()) && StringUtils.isEmpty(cpr.getIssueDateEnd())) {
			cpr.setIssueDateEnd(DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN));
		}
		return constructionPermitRecordMapper.selectConstructionPermitRecordTable(cpr);
	}

	@Override
	public List<CrOperaterecordinfo> selectConstructionPermitJournal(String objectid) {
		BaseUnitInfo baseUnitInfo = new BaseUnitInfo();
		List<CrOperaterecordinfo> coiList = constructionPermitRecordMapper.selectCrOperaterecordinfoByObjectId(objectid);
		for(CrOperaterecordinfo coi:coiList) {
			BaseUnitUserInfo user = constructionPermitRecordMapper.selectBaseUnitUserInfoByLoginId(coi.getOperator());
			if(StringUtils.isNotNull(user)) {
				baseUnitInfo.setGuid(user.getUnitInfoGuid());
				BaseUnitInfo unitInfo = baseUnitInfoMapper.selectBaseUnitInfoByGuid(baseUnitInfo);
				if(StringUtils.isNotNull(unitInfo)) {
					coi.setCompanyName(unitInfo.getUnitName());
				}
				coi.setDept(user.getDept());
			}else {
				BaseUnitUserInfo uniUser = constructionPermitRecordMapper.selectBaseUnitUserInfoByLoginId(coi.getOperator());
				if(StringUtils.isNotNull(uniUser)) {
					if(StringUtils.isNotEmpty(uniUser.getUnitInfoGuid())) {
						BaseBulidUnitInfo bbui = enterpriseMapper.selectBaseBulidUnitInfoByGuid(uniUser.getUnitInfoGuid());
						coi.setCompanyName(bbui.getUnitName());
					}
				}else {
					BaseCompanyUserInfo comUser = constructionPermitRecordMapper.selectBaseCompanyUserInfoByLoginId(coi.getOperator());
					if(StringUtils.isNotNull(comUser)) {
						if(StringUtils.isNotEmpty(comUser.getCompanyGuid())) {
							Enterprise enterprise = enterpriseMapper.selectEnterpriseInfoByGuid(comUser.getCompanyGuid());
							coi.setCompanyName(enterprise.getCompanyName());
						}
					}
				}
			}
		}
		return coiList;
	}

}
